﻿using System;
using System.Windows.Input;

internal class KbInputHandler : InputHandler
{
    internal bool[] allKeysState;
    internal int id;
    internal int[] keyMapping;
    internal NES nes;

    internal KbInputHandler(NES nes, int id)
    {
        this.nes = nes;
        this.id = id;
        this.allKeysState = new bool[0xff];
        this.keyMapping = new int[InputHandler.NUM_KEYS];
    }

    internal override short getKeyState(int padKey)
    {
        return (this.allKeysState[this.keyMapping[padKey]] ? ((short) 0x41) : ((short) 0x40));
    }

    internal virtual void keyPressed(object event_sender, KeyEventArgs ke)
    {
        int index = (int)ke.Key;
        if (index < this.allKeysState.Length)
        {
            this.allKeysState[index] = true;
            if (index == this.keyMapping[InputHandler.KEY_LEFT])
            {
                this.allKeysState[this.keyMapping[InputHandler.KEY_RIGHT]] = false;
            }
            else if (index == this.keyMapping[InputHandler.KEY_RIGHT])
            {
                this.allKeysState[this.keyMapping[InputHandler.KEY_LEFT]] = false;
            }
            else if (index == this.keyMapping[InputHandler.KEY_UP])
            {
                this.allKeysState[this.keyMapping[InputHandler.KEY_DOWN]] = false;
            }
            else if (index == this.keyMapping[InputHandler.KEY_DOWN])
            {
                this.allKeysState[this.keyMapping[InputHandler.KEY_UP]] = false;
            }
        }
    }

    internal virtual void keyReleased(object event_sender, KeyEventArgs ke)
    {
        int index = (int)ke.Key;
        if (index < this.allKeysState.Length)
        {
            this.allKeysState[index] = false;
            if (this.id == 0)
            {
                switch (index)
                {
                    //case 0x29:
                    //    this.nes.stateLoad();
                    //    break;

                    //case 0x30:
                    //    this.nes.stateSave();
                    //    break;
                }
            }
        }
    }

    internal override void mapKey(int padKey, int kbKeycode)
    {
        this.keyMapping[padKey] = kbKeycode;
    }

    internal override void reset()
    {
        this.allKeysState = new bool[0xff];
    }

    internal override void update()
    {
        
    }
}

